package com.a214.yxz.yuejian013.Activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Base.BaseSActivity;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;

public class UserSettingChangePwdActivity extends BaseSActivity {

    private ImageButton return_btn;
    private TextView save;
    private EditText new_pwd, check_pwd;
    private ResModel model = new ResModel("", getSessionID());
    private ServiceListener<String> listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_setting_change_pwd);

        return_btn = (ImageButton) findViewById(R.id.user_setting_changePwd_return_btn);
        save = (TextView) findViewById(R.id.user_setting_changePwd_save);
        new_pwd = (EditText) findViewById(R.id.user_setting_changePwd_newPwd);
        check_pwd = (EditText) findViewById(R.id.user_setting_changePwd_checkNewPwd);

        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        listener = new ServiceListener<String>() {
            @Override
            public void onResponse(String l) {
                if (l.equals("1")) {
                    Toast.makeText(UserSettingChangePwdActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(UserSettingChangePwdActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (new_pwd.getText().toString().equals(check_pwd.getText().toString())) {
                    SharedPreferences sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);
                    model.updateUserPass(sharedPreferences.getString("user_id","408"), new_pwd.getText().toString(), listener);
                } else {
                    Toast.makeText(UserSettingChangePwdActivity.this, "两次输入密码不一致！", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
